using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class HoverText : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    [Header("关卡编号")] [SerializeField] private int _levelID;

    private TMP_Text itemRequirementText;
    private int _itemID;
    private Button _button;
    private void Awake()
    {
        _itemID = Constants.LEVEL_ITEM_REQUIREMENT[_levelID];
        itemRequirementText = transform.GetChild(1).GetComponent<TMP_Text>();
        itemRequirementText.text = "需要道具：\n【" + InventoryManager.Instance.itemDatas.itemDatas[_itemID].itemName+"】";
        _button = GetComponent<Button>();
        _button.onClick.AddListener(() =>
        {
            transform.GetChild(1).gameObject.SetActive(false);
        });
    }

    private void OnEnable()
    {
        bool _hasItem = InventoryManager.Instance.HasItem(_itemID);
        _button.interactable = _hasItem;
        itemRequirementText.color = _hasItem ? Color.green : Color.red;
        GetComponent<Image>().material.SetFloat("_Locked", _hasItem ? 0 : 1);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Enter");
        /*if (InventoryManager.Instance.HasItem(_itemID))
        {
            itemRequirementText.color = Color.green;
        }
        else
        {
            itemRequirementText.color = Color.red;
        }*/
        itemRequirementText.gameObject.SetActive(true);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        itemRequirementText.gameObject.SetActive(false);
    }
}
